package sys.Message_Sysytem;

import sys.Message_Sysytem.bean.Message;
import sys.Message_Sysytem.dao.MessageDao;
import sys.Message_Sysytem.dao.impl.MessageDaoImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = 0;
        MessageDao messageDao = new MessageDaoImpl();
        do {
            System.out.println("1.查看所有留言 2.添加留言 3.退出");
            System.out.println("请选择功能：");
            num = input.nextInt();
            switch (num){
                case 1:
                    List<Message> all = messageDao.findAll();
                    System.out.println("序号\t\t留言内容\t\t日期");
                    for (int i =0;i < all.size();i++){
                        System.out.println(all.get(i).getId()+"\t\t"+all.get(i).getMessage()+"\t\t"+all.get(i).getTime());
                    }
                    break;
                case 2:
                    Message message = new Message();
                    System.out.println("请输入留言内容：");
                    //吸收nextInt()留下来的回车
                    String messageContent = input.nextLine();
                    messageContent = input.nextLine();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 'at' hh:mm:ss");
                    Date date = new Date();
                    String nowTime = simpleDateFormat.format(date);
                    message.setMessage(messageContent);
                    message.setTime(nowTime);
                    int count = messageDao.addMessage(message);
                    if (count>0){
                        System.out.println("留言成功！");
                    }
                    break;
                case 3:
                    break;
            }
        }while (num!=3);
        System.out.println("谢谢使用！");
    }
}
